#pragma once

#include "../include/TcpServer.hpp"


class EchoServer
{
private:
    void OnConnected(const PtrConnection& conn)
    {
        // LOG(DEBUG, "NEW CONNECTION: %d\n", conn->GetFD());
    }

    void OnClosed(const PtrConnection& conn)
    {
        // LOG(DEBUG, "RELEASE CONNECTION: %d\n", conn->GetFD());
    }

    void OnMessage(const PtrConnection& conn, Buffer* buf)
    {
        // LOG(INFO, "Client: %s\n", buf->GetReadPos());
        std::string str;
        buf->ReadAsStringAndPop(&str, buf->GetSizeOfReadableData());
        conn->Send(str.c_str(), str.size());
        // conn->Shutdown();
    }

public:
    EchoServer(in_port_t port)
        : _server(port)
    {
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        
        _server.EnableInactiveRelease(10);
        _server.SetThreadCount(5);
    }

    void Start()
    {
        LOG(INFO, "Server Start!\n");
        _server.Start();
    }

private:
    TcpServer _server;
};